package com.uuah.server.context;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.uuah.server.UuahServer;
import com.uuah.server.api.IUuahContext;
import com.uuah.server.api.agent.IAgent;
import com.uuah.server.api.context.notification.linstener.IAgentMBeanNotificationListener;
import com.uuah.server.api.context.notification.linstener.IConnectionNotificationListener;
import com.uuah.server.api.context.notification.linstener.IServerNotificationListener;
import com.uuah.server.api.context.notification.linstener.IServiceNotificationListener;
import com.uuah.server.api.context.notification.linstener.IUuahContextNotificationListener;
import com.uuah.server.config.Constants;
import com.uuah.server.context.notification.AgentMBeanNotification;
import com.uuah.server.context.notification.ServerNotification;
import com.uuah.server.exception.context.notification.NotificationException;

/**
 *
 * TODO comment ...
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午01:35:30
 * @version 1.0.0
 */
public class UuahEventLinstenerBuilder {
	private static Logger logger = LoggerFactory
			.getLogger(UuahEventLinstenerBuilder.class);

	public static void registerEventListener() {
		if (logger.isDebugEnabled()) {
			logger.debug("Register Event Listeners [Log Manage]...");
		}
		IUuahContext uuahContext = UuahServer.getUuahContext();

		IServerNotificationListener l = new IUuahContextNotificationListener() {
			public void onNotification(ServerNotification notification) {
				logEvent(notification);
			}
		};
		try {
			uuahContext.registerListener(l);
		} catch (NotificationException e) {
			logger.error(e.getMessage());
		}
		l = new IAgentMBeanNotificationListener() {
			public void onNotification(ServerNotification notification) {
				logEvent(notification);
				IAgent agent = (IAgent) UuahServer.getUuahContext()
						.getRegistry()
						.lookupObject(Constants.OBJECT_MX4J_AGENT);
				if (agent != null) {
					AgentMBeanNotification agentMBeanNotification = (AgentMBeanNotification) notification;
					if (agentMBeanNotification.getAction() == AgentMBeanNotification.CREATE_MBEAN_ACTION) {
						try {
							agent.registerMBean(agentMBeanNotification
									.getMbeanObj(), agentMBeanNotification
									.getObjectName(), agentMBeanNotification
									.isDynamic());
						} catch (Exception e) {
							logger.error("", e);
						}

					} else if (agentMBeanNotification.getAction() == AgentMBeanNotification.DELETE_MBEAN_ACTION) {
						try {
							agent.unregisterMBean(agentMBeanNotification
									.getObjectName());
						} catch (Exception e) {
							logger.error("", e);
						}
					}
				}
			}
		};
		try {
			uuahContext.registerListener(l);
		} catch (NotificationException e) {
			logger.error(e.getMessage());
		}

		l = new IServiceNotificationListener() {
			public void onNotification(ServerNotification notification) {
				logEvent(notification);
			}
		};
		try {
			uuahContext.registerListener(l);
		} catch (NotificationException e) {
			logger.error(e.getMessage());
		}
		l = new IConnectionNotificationListener() {
			public void onNotification(ServerNotification notification) {
				logEvent(notification);
			}
		};
		try {
			uuahContext.registerListener(l);
		} catch (NotificationException e) {
			logger.error(e.getDetailedMessage());
		}
		l = new IUuahContextNotificationListener() {
			public void onNotification(ServerNotification notification) {
				logEvent(notification);
			}
		};
		try {
			uuahContext.registerListener(l);
		} catch (NotificationException e) {
			logger.error(e.getDetailedMessage());
		}
	}

	protected static void logEvent(ServerNotification e) {
		logger.debug("===Event Notification Log:" + e);
	}

}
